//
//  main.m
//  lzl5-9
//
//  Created by 刘振霖 on 16/5/9.
//  Copyright © 2016年 lzl. All rights reserved.
//

#import <Foundation/Foundation.h>


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        
        NSString * path = @"/Users/liuzhenlin/Downloads/00001.vcf";
        NSError * err;
        NSString * vcf = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        if(err!=nil){
            NSLog(@"%@",err);
        }
        //NSLog(@"%@",vcf);
        
        NSArray * arr = [vcf componentsSeparatedByString:@"\n"];
        
        NSMutableArray * res = [NSMutableArray new];
        
        NSMutableDictionary * dict;
        
        for(NSString * line in arr){
//            NSLog(@"%@",line);
            
            if([line hasPrefix:@"BEGIN"]){
                NSLog(@"开始一条新纪录");
                dict = [NSMutableDictionary new];
            }
            if([line hasPrefix:@"END"]){
                NSLog(@"这条记录结束了");
                [res addObject:dict];
            }
            if([line hasPrefix:@"TEL"]){
                NSLog(@"找到一条电话记录");
                NSArray * tels = [line componentsSeparatedByString:@"tel"];
                NSString * last = tels.lastObject;
//                NSInteger len = last.length;
//                len -= 5;
                
                last = [last substringFromIndex:5];
//                last = [last stringByReplacingOccurrencesOfString:@"CELL" withString:@""];
                NSLog(@"%@",last);
                dict[@"tel"] = last;
                
            }
            if([line hasPrefix:@"FN"]){
                NSLog(@"找到了一个FN，也就是姓名:%@",line);
                
                NSArray * names = [line componentsSeparatedByString:@":"];
                NSString * name = names.lastObject;
                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                name = name.stringByRemovingPercentEncoding;
                NSLog(@"%@",name);
                dict[@"name"] = name;
            }
            
        }
        NSLog(@"%@",res);
        [res  writeToFile:@"/Users/liuzhenlin/Desktop/res.text" atomically:NO];
        
        /*
        NSString * path2 = @"/Users/liuzhenlin/Desktop/复习";
        NSError * err;
        NSString * vcf = [[NSString alloc]initWithContentsOfFile:path2 encoding:NSUTF8StringEncoding error:&err];
        if(err != nil){
            NSLog(@"%@",err);
        }
        NSLog(@"%@",vcf);
        */
        /*
        
        NSString * path3 = @"/Users/liuzhenlin/Downloads/00001.vcf";
        NSError * err3;
        NSString * vcf3 = [[NSString alloc]initWithContentsOfFile:path3 encoding:NSUTF8StringEncoding error:&err3];
        
        if(err3 != nil){
            NSLog(@"%@",err3);
        }
        NSLog(@"%@",vcf3);
        
        
        NSArray * arr = [vcf3 componentsSeparatedByString:@"\n"];
        for(NSString *line in arr){
            if([line hasPrefix:@"BEGIN"]){
                NSLog(@"开始查找电话");
            }
            if([line hasPrefix:@"END"]){
                NSLog(@"结束查找");
            }
            if([line hasPrefix:@"TEL"]){
                NSLog(@"找到了一条电话%@",line);
                NSArray * tels = [line componentsSeparatedByString:@"tel"];
                
            
            }
        }
        
        
        */
        
        
        
        
        
        
        
        
    }
    return 0;
}
